package com.rylan.controller;

import com.rylan.entity.Room;
import com.rylan.entity.RoomVO;
import com.rylan.feign.RoomFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;



import java.util.List;

/**
 * @author rylan
 * @description
 */
@Controller
@RequestMapping("/room")
public class RoomController {
    @Autowired
    private RoomFeign roomFeign;

    @GetMapping("/findAll")
    @ResponseBody
    public RoomVO findAll(@RequestParam("page") int page, @RequestParam("limit") int limit){
        int index = (page-1)*limit;
        List<Room> data = roomFeign.findAll(index,limit);
        int count = roomFeign.findAll(0,1000).size();
        return new RoomVO(0,"",count,data);
    }
    @GetMapping("/deleteById/{id}")
    public String deleteById(@PathVariable("id") long id){
        roomFeign.deleteById(id);
        return "redirect:/room/mahjong/index";
    }

    @GetMapping("/mahjong/{location}")
    public String redirect(@PathVariable("location") String location){
        return location;
    }

    @GetMapping("/findTypes")
    public String findTypes(Model model){
        model.addAttribute("list",roomFeign.findTypes());
        return "room_add";
    }
    @PostMapping("/save")
    public String save(Room room){
        roomFeign.save(room);
        return "redirect:/room/mahjong/index";
    }
    @GetMapping("/findById/{id}")
    public String findById(Model model,@PathVariable("id") long id){
        model.addAttribute("room",roomFeign.findById(id));
        model.addAttribute("list",roomFeign.findTypes());
        return "room_update";
    }
    @PostMapping("update")
    public String update(Room room){
        roomFeign.update(room);
        return "redirect:/room/mahjong/index";
    }
}
